package cj.web.admin.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.constants.SourceEnum;
import cj.web.admin.dao.ContantDAO;
import cj.web.admin.domain.Contant;
import cj.web.admin.domain.Industry;
import cj.web.login.controllers.BaseController;

@Path("/contant")
public class ContantController extends BaseController{

	private static Logger logger = Logger.getLogger(ContantController.class.getName());
	
	@Autowired
	private ContantDAO contantDao;
	
	@Get("show")
	public String show(Model model,@Param("category") int category)
	{
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        
        model.add("category",category);
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
        //MemuEnum
        model.add("MemuEnum",retMemuValue(category));
		return "/views/portal/contantList.vm";
	}

	@Post("getList")
	public String getUserList(Contant contant) {
		
		JSONObject jo = new JSONObject();
		
		List<Contant> objs = contantDao.query(contant);
		
		int size = contantDao.querysize(contant);
		jo.put("data", objs);
		jo.put("draw", contant.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("queryById")
	public String queryById(Model model,@Param("id") int id)
	{
		contantDao.queryById(id);
		return null;
	}
	
	@Get("createPage")
	public String createPage(Model model,@Param("category") int category)
	{
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        
        model.add("category",category);
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
        //MemuEnum
        model.add("MemuEnum",retMemuValue(category));
		return "/views/portal/contantDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Contant contant,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			
			contant.setCreateUser(returnSessionObj(request).getAccount());
			contant.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			contant.setUpdateUser(returnSessionObj(request).getAccount());
			contant.setContent(umContent);
			contant.setReleaseState(Constants.portal.releaseState.state_1);
			contant.setReleaseAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
			contantDao.create(contant);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Contant obj = contantDao.queryById(id);
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
		model.add("contant", obj);
		//MemuEnum
        model.add("MemuEnum",retMemuValue(Integer.parseInt(obj.getCategory())));
		return "/views/portal/contantEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Contant contant,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			contant.setContent(umContent);
			contant.setUpdateUser(returnSessionObj(request).getAccount());
			contant.setReleaseState(Constants.portal.releaseState.state_1);
			contant.setReleaseAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
			contantDao.update(contant);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		Contant obj = contantDao.queryById(id);
		
		if(obj!=null)
		{
			contantDao.deleteById(id);
		}
		
		return "r:show?category="+obj.getCategory();
	}
	
	@Get("copyById")
	public String copyById(Model model,@Param("id") int id)
	{
		model.add("categoryEnum", EnumSet.allOf(CategoryEnum.class) );
		
		Contant obj = contantDao.queryById(id);
		model.add("Obj", obj);
		//MemuEnum
        model.add("MemuEnum",retMemuValue(Integer.parseInt(obj.getCategory())));
		return "portal/contantCopy.vm";
	}
	
	@Post("saveCopyById")
	public String saveCopyById(Model model,@Param("id") int id,@Param("category") int category)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		Contant obj = contantDao.queryById(id);
		if(obj!=null)
		{
			contantDao.createCoptById(id, category);
		}
		
		return "@" + jo.toString();
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 162, 243, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	@Get("fileDelete")
    public String fileDelete(@Param("key") String key) {
		JSONObject jo = new JSONObject();
		jo.put("key", key);
		jo.put("id", key);
		return "@"+jo.toString();
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") int id) {
		
		Contant obj = contantDao.queryById(id);
		
		if(obj!=null)
		{
			contantDao.updateByStickTime(id);
		}
		
		return "r:show?category="+obj.getCategory();
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") int id) {
		
		Contant obj = contantDao.queryById(id);
		
		if(obj!=null)
		{
			contantDao.updateByStickTimeIsNull(id);
		}
		
		return "r:show?category="+obj.getCategory();
	}
	
	/**
	 * 返回菜单
	 * @param category
	 * @return
	 */
	private MemuEnum retMemuValue(int category)
	{
		if(category==0)
		{
			return MemuEnum.cjtt;
		}
		if(category==1)
		{
			return MemuEnum.wjh;
		}
		if(category==2)
		{
			return MemuEnum.jtdt;
		}
		if(category==3)
		{
			return MemuEnum.pphd;
		}
		
		if(category==4)
		{
			return MemuEnum.cjrbzxdt;
		}
		if(category==5)
		{
			return MemuEnum.cjrbpphd;
		}
		if(category==6)
		{
			return MemuEnum.cjrbpplm;
		}
		if(category==7)
		{
			return MemuEnum.cjrbppzk;
		}
		
		return null;
	}
}
